/*
 * Copyright (c) Zhongkehonglue Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Implementation of functions related to the DMC DEV module.
 * Author: wangchunyang
 * Create: 2025-05-23
 */

#include "dmcApi.h"
#include "dmcLog.h"

#include "dmcEnv.h"
#include "dmcAvt.h"
#include "dmcRc.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

namespace DlnaDmc {

dmc_int32 DmcApi::InitEnv(const std::string &ifName, const DmcCbType &cb)
{
    LOGI("init env begin.");
    auto ret = DmcEnv::GetInstance().InitEnv(ifName, cb);
    if (ret != DMC_SUCCESS) {
        LOGE("init env failed.");
    } else {
        LOGI("init env succeeded.");
    }
    return ret;
}

dmc_int32 DmcApi::DestroyEnv()
{
    LOGI("destroy env begin.");
    auto ret = DmcEnv::GetInstance().DestroyEnv();
    if (ret != DMC_SUCCESS) {
        LOGE("destroy env failed.");
    } else {
        LOGI("destroy env succeeded.");
    }
    return ret;
}

dmc_int32 DmcApi::SetAvTransportURI(dmc_uint32 deviceId, dmc_uint32 instanceId,
    const std::string &uri, const std::string &metaData)
{
    LOGI(" set avt url begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    return DmcSetAvTransportURI(deviceId, instanceId, uri, metaData);
}

dmc_int32 DmcApi::Play(dmc_uint32 deviceId, dmc_uint32 instanceId, const std::string &playSpeed)
{

    LOGI(" play begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    return DmcPlay(deviceId, instanceId, playSpeed);
}

dmc_int32 DmcApi::Pause(dmc_uint32 deviceId, dmc_uint32 instanceId)
{
    LOGI(" pause begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    return DmcPause(deviceId, instanceId);
}

dmc_int32 DmcApi::Stop(dmc_uint32 deviceId, dmc_uint32 instanceId)
{
    LOGI(" Stop begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    return DmcStop(deviceId, instanceId);
}

dmc_int32 DmcApi::Seek(dmc_uint32 deviceId, dmc_uint32 instanceId, DmcSeekMode seekMode,
    const std::string &seekTarget)
{
    LOGI("Seek begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    auto ret = DmcSeek(deviceId, instanceId, seekMode, seekTarget);
    if (ret != DMC_SUCCESS) {
        LOGE("dmc seeek failed.");
    } else {
        LOGI("dmc seek succeeded.");
    }
    return ret;
}

dmc_int32 DmcApi::SetVolume(dmc_uint32 deviceId, dmc_uint32 instanceId,
    DmcChannelType enChannel, dmc_uint32 volume)
{
    LOGI(" set volume begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE(" not inited.");
        return DMC_NOT_INITED;
    }
    return DmcSetVolume(deviceId, instanceId, enChannel, volume);
}

dmc_int32 DmcApi::GetVolume(dmc_uint32 deviceId, dmc_uint32 instanceId,
    DmcChannelType enChannel, dmc_uint32 &currentVolume)
{
    LOGI("get volume begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE("not inited.");
        return DMC_NOT_INITED;
    }
    auto ret = DmcGetVolume(deviceId, instanceId, enChannel, currentVolume);
    if (ret != DMC_SUCCESS) {
        LOGE("get volume failed.");
    } else {
        LOGI("get volume succeeded.");
    }
    return ret;
}
dmc_int32 DmcApi::SetMute(dmc_uint32 deviceId, dmc_uint32 instanceId,
    DmcChannelType enChannel, bool mute)
{
    LOGI("set mute begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE("not inited.");
        return DMC_NOT_INITED;
    }
    auto ret = DmcSetMute(deviceId, instanceId, enChannel, mute);
    if (ret != DMC_SUCCESS) {
        LOGE("set mute failed.");
    } else {
        LOGI("set mute succeeded.");
    }
    return ret;
}

dmc_int32 DmcApi::GetPositionInfo(dmc_uint32 deviceId, dmc_uint32 instanceId, DmrTrackPosition &trackPos)
{
    LOGI("Get position info begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE("not inited.");
        return DMC_NOT_INITED;
    }
    auto ret = DmcGetPositionInfo(deviceId, instanceId, trackPos);
    if (ret != DMC_SUCCESS) {
        LOGE("Get position info failed.");
    } else {
        LOGI("Get position info succeeded.");
    }
    return ret;
}

dmc_int32 DmcApi::GetTransportInfo(dmc_uint32 deviceId, dmc_uint32 instanceId,
    DmcTransportState &transportState, DmcTransportStatus &transportStatus, std::string &playSpeed)
{
    LOGI("Get transport info begin.");
    if (!DmcEnv::GetInstance().IsInited()) {
        LOGE("not inited.");
        return DMC_NOT_INITED;
    }
    auto ret = DmcGetTransportInfo(deviceId, instanceId, transportState, transportStatus, playSpeed);
    if (ret != DMC_SUCCESS) {
        LOGE("Get transport info failed.");
    } else {
        LOGI("Get transport info succeeded, transportState=%{public}d, \
            transportStatus=%{public}d, playSpeed=%{public}s",
            transportState, transportStatus, playSpeed.c_str());
    }
    return ret;
}
} // DlnaDmc




